#!/usr/bin/perl -w


open(FILE, $ARGV[0]) or die "Unable to open $ARGV[0]";
$fovline[0] = "";
while (<FILE>) {
         # $1        $2  $3 
    if (/^(i.*v)180(.*n"(.*)")$/) {
        @fovline = `fov.pl $3`;
        chomp @fovline;
        for($i=0; $i < @fovline; $i++) {
            $fovline[$i] = $1.$fovline[$i].$2;
        }
        last;
    }
}

close(FILE);

for($i=0; $i<@fovline; $i++) {
open(FILE, $ARGV[0]) or die "Unable to reopen $ARGV[0]";
    $outfile = "";
    if ($ARGV[0] =~ /(.*)\.pto/) {
        $outfile = $1 . "-fov-" ."$i".".pto"
    }

    open(OUT, ">$outfile") or die "Unable to open $outfile for write";
    while(<FILE>) {
        if (/^i.*v180.*n".*"$/) {
            print OUT $fovline[$i], "\n";
        } else {
            print OUT $_;
        }
    }
    close(OUT);
    print $outfile, "\n";
}

close(FILE);
